<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2018/12/17
 * Time: 10:36
 */
namespace app\house\controller;

use app\common\controller\Frontend;
use app\house\model\Building as BuildingModel;
use app\house\model\BuildingInfo as BuildingInfoModel;
use app\house\model\Car as CarModel;
use app\house\model\School as SchoolModel;
use QL\QueryList;
use think\Config;
use think\Cache;

class Index extends Frontend {
    protected $noNeedLogin = ['index','item','movie','car','carCollect','schoolCollect','school'];
    protected $layout = 'default';


    public function index(){
        $this->assign('title','楼盘');
        $BuildingModel = new BuildingModel();
        $list = $BuildingModel->paginate(15);
        $this->assign('list',$list);
        return $this->view->fetch();
    }

    public function movie(){
        $this->assign('title','电影票房');
        /*$content = Cache::get('movie');
        if(!$content) {*/
            $url = Config::get('movie_url');
            $content = QueryList::get($url)->find('#content')->html();
          /*  Cache::set('movie',$content,10);
        }*/
        $this->assign('content', $content);
        return $this->view->fetch();
    }

    public function school(){
        $this->assign('title','大学排名');
        $SchoolModel = new SchoolModel();
        $list = $SchoolModel->order('ranking asc')->paginate(15);
        $this->assign('list',$list);
        return $this->view->fetch();
    }

    public function schoolCollect(){
        set_time_limit(0);
        $this->assign('title','大学排名');
        for($i = 1 ;$i< 73;$i++){
            $url = "https://data-gkcx.eol.cn/soudaxue/queryschool.html?messtype=json&page={$i}&size=40";
            $list = json_decode($this->http_get($url),true);
            $schoolModel = new SchoolModel();
            foreach ($list['school'] as $item){
                unset($item['clicks']);
                unset($item['monthclicks']);
                unset($item['weekclicks']);
                unset($item['firstrateclass']);
                $schoolModel->insert($item);
            }
        }

        /*$this->assign('content', $content);
        return $this->view->fetch();*/
    }

    public function http_get($url)
    {
        $header = [
            'Accept:*/*',
            'Accept-Encoding:gzip, deflate, br',
            'Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Connection:keep-alive',
            'Cookie:tool_ipuse=117.71.49.219; tool_ipprovince=34; tool_iparea=3',
            'Host:data-gkcx.eol.cn',
            'Referer:https://gkcx.eol.cn/soudaxue/queryschool.html?&page=1',
            'User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0',
        ];
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        return $tmpInfo;    //返回json对象

    }

#采集接口
    public function carCollect(){
        $this->assign('title','车标');
        $url = Config::get('car_url');
        $carModel = new CarModel();
        for($i=ord('a'); $i<=ord('z'); $i++){
            if(chr($i) == 'u'){continue;}
            $url1 = $url.chr($i).'/';
            $rules = [
                'img' => ['.zt_zimu > li > a > img ','src'],
                'chinese' => ['.zt_zimu > li > a > p > span','text'],
            ];
            $content = QueryList::get($url1)->rules($rules)->query()->getData();
            foreach ($content as $item) {
                $data = [
                    'chinese' => iconv("GB2312","UTF-8",$item['chinese']),
                    'img'     => !empty($item['img'])?$item['img']:'',
                    'flag'    => chr($i),
                ];
                $carModel->insert($data);
            }
        }
        //$this->assign('content', $content);
        //return $this->view->fetch();
    }

    public function car(){
        $this->assign('title','车标');
        $carModel = new CarModel();
        $carList = $carModel->paginate(6);
        $this->assign('list', $carList);
        return $this->view->fetch();
    }



    public function item(){
        $id = $this->request->param('id');
        $buildingInfo = new BuildingInfoModel();
        $info = $buildingInfo->alias('a')->join('fa_building b ','a.build_id = b.build_id ')->where(['a.build_id'=>$id])->field('a.* ,b.build_name')->find();
        $this->assign('title',$info['build_name']);
        $this->assign('info',$info);
        return $this->view->fetch();
    }
}